//有4组数据
//1.excel中品牌数据 品牌名称以及关联的店铺名称
//2.excel中店铺数据 店铺的名称以及详细数据
//3.远程品牌数据 会有品牌ID 以及 关联的店铺ID
//4.远程店铺数据 有店铺ID
//5.组合品牌数据，几个品牌组合在一起，里面店铺合并，但是从属还是各自的，用于页面显示
//远程的数据中有ID，但是关联可能有误差
//excel中的关联最重要
//本质上使用excel中的数据来查找对应的远程数据中的品牌ID以及店铺ID即可
import { zuhe_brand, local_brand, local_shop, remote_brand, remote_shop } from './json/local_brand.js'
export default class BrandShop {
  constructor() {
    this.local_brand = []
    this.zuhe_brand = []
    this.init()
  }
  /**
   * 初始化表格数据以及显示的数据.
   */
  init() {
    let brands = []
    brands = local_brand.map((res) => {
      let id = this.findBrandIdByBrandName(res.brand)
      let brandName = res.brand
      res.id = id
      let shopInfo = this.findShopByBrandName(brandName)
      res.shops = shopInfo.child.map((res) => {
        let shopId = this.findShopIdByShopName(res.department)
        res.shopId = shopId
        res.brandId = res.brandId || id
        res.brandName = brandName
        return res
      })
      return res
    })
    this.local_brand = brands
    this.zuhe_brand = zuhe_brand.map((res) => {
      let shops = []
      if (typeof res.brand == 'object') {
        res.brand.forEach((res) => {
          let brandShops = this.findLocalShopsByName(res)
          shops = shops.concat(brandShops)
        })
      } else {
        let brandShops = this.findLocalShopsByName(res.brand)
        shops = shops.concat(brandShops)
      }
      res.shops = shops
      return res
    })
  }
  /**
   * 查找本地店铺数据通过品牌名称.
   */
  findLocalShopsByName(brand) {
    let item = this.local_brand.find((res) => res.brand == brand)
    if (item) return item.shops
    else {
      console.log(`未找到品牌数据local`, brand)
      return []
    }
  }
  /**
   * 通过店铺名称到远程查找店铺数据
   */
  findShopIdByShopName(shopName) {
    let shop = remote_shop.find((res) => res.name == shopName)
    if (shop) {
      return shop.id
    } else {
      console.log(`未找到店铺数据`, shopName)
    }
  }
  /**
   * 通过品牌名对应店铺数据.
   *
   * @param {} brand
   */
  findShopByBrandName(brand) {
    let brandInfo = local_shop.find((res) => res.brand == brand)
    if (brandInfo) {
      return brandInfo
    } else {
      console.log(`未找到品牌店铺数据`)
    }
  }
  /**
   * 通过品牌名查找远程数据ID.
   *
   * @param {} brand
   */
  findBrandIdByBrandName(brand) {
    let item = remote_brand.find((res) => res.name == brand)
    if (item) {
      return item.id
    } else {
      console.log(`没有找到品牌`, brand)
    }
  }
}
